<?php

namespace App\Http\Controllers\Admin;

use App\Models\OperatorLog;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class historyController extends Controller
{
   
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
		$uid = auth()->user()->id;
        if ($request->ajax()) {
            $data = array();
            $data['draw'] = $request->get('draw');
            $start = $request->get('start');
            $length = $request->get('length');
            $order = $request->get('order');
            $columns = $request->get('columns');
            $search = $request->get('search');
            $data['recordsTotal'] = OperatorLog::where(function($query) use ($uid){
					if($uid!=1)$query->where('uid','=',$uid);
				})->count();
            if (strlen($search['value']) > 0) {
                $data['recordsFiltered'] = OperatorLog::where(function ($query) use ($search) {
                    $query->where('operator_log.content', 'LIKE', '%' . $search['value'] . '%')
                        ->orWhere('operator_log.type', 'like', '%' . $search['value'] . '%');
                })
				->where(function($query) use ($uid){
					if($uid!=1)$query->where('uid','=',$uid);
				})->count();
                $data['data'] = OperatorLog::select('operator_log.*','admin_users.name as uname')
				->leftJoin('admin_users','admin_users.id','=','operator_log.uid')
                ->where(function ($query) use ($search) {
                    $query->where('operator_log.content', 'LIKE', '%' . $search['value'] . '%')
                        ->orWhere('operator_log.type', 'like', '%' . $search['value'] . '%');
                })
					->where(function($query) use ($uid){
						if($uid!=1)$query->where('uid','=',$uid);
					})->skip($start)->take($length)
                    ->orderBy($columns[$order[0]['column']]['data'], $order[0]['dir'])
                    ->get();
            } else {
                $data['recordsFiltered'] = $data['recordsTotal'];//OperatorLog::whereIn('uid',session('pids'))->count();
                $data['data'] = OperatorLog::select('operator_log.*','admin_users.name as uname')
				->leftJoin('admin_users','admin_users.id','=','operator_log.uid')
				->where(function($query) use ($uid){
					if($uid!=1)$query->where('uid','=',$uid);
				})
				->skip($start)->take($length)
                    ->orderBy($columns[$order[0]['column']]['data'], $order[0]['dir'])
                    ->get();
            }

            return response()->json($data);
        }

        return view('admin.history.index');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
       
    }

    /**
     * Display the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
      
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
		if(is_numeric($id)){
            $tag = OperatorLog::find((int)$id);
			if ($tag ) {
				event(new \App\Events\userActionEvent('\App\Models\History', $tag->id, 3, '删除了操作记录' . $tag->name));
				$tag->delete();
			} else {
				return redirect()->back()
					->withErrors("删除失败");
			}
        }else{
            $idArr = explode(',',$id);
			foreach($idArr as $ido){
				$tag = OperatorLog::find((int)$ido);
				if ($tag ) {
					event(new \App\Events\userActionEvent('\App\Models\History', $tag->id, 3, '删除了操作记录' . $tag->name));
					$tag->delete();
				} else {
					return redirect()->back()
						->withErrors("删除失败");
				}
			}
        }
        

        return redirect()->back()
            ->withSuccess("删除成功");
    }

}
